import { reduce } from 'lodash'

const numRef = [
  {
    num: 1,
    word: 'Error'
  },
  {
    num: 2,
    word: 'Warn'
  },
  {
    num: 3,
    word: 'Log'
  },
  {
    num: 4,
    word: 'Debug'
  },
  {
    num: 0,
    word: 'Off'
  }
]

function numToWord(num: number) {
  return reduce(
    numRef,
    (accum, ref) => {
      return ref.num === num ? ref.word : accum
    },
    ''
  )
}

function wordToNum(word: string) {
  return reduce(
    numRef,
    (accum, ref) => {
      return ref.word === word && word.toLowerCase() ? ref.num : accum
    },
    -1
  )
}

export default class LogUtil {
  debugLevel: number
  debugName: string
  constructor(debugLevel = 3) {
    this.debugLevel = debugLevel
    this.debugName = numToWord(debugLevel)

    this.log('LogUtil initialized', this.debugName, wordToNum(this.debugName))
  }
  debug(...args: any[]) {
    if (this.debugLevel >= 5) {
      console.debug(...args)
    }
  }
  log(...args: any[]) {
    if (this.debugLevel >= 3) {
      console.log(...args)
    }
  }
  warn(...args: any[]) {
    if (this.debugLevel >= 2) {
      console.warn(...args)
    }
  }
  error(...args: any[]) {
    if (this.debugLevel >= 1) {
      console.error(...args)
    }
  }
}
